package org.wmq.shopmanager.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.commonutil.context.SystemUserHolder;
import org.wmq.commonutil.result.Result;
import org.wmq.shopmanager.service.SystemMenuService;
import org.wmq.shopmanager.service.SystemUserService;
import org.wmq.shopmanager.service.ValidateCodeService;
import org.wmq.shopmodel.DTO.system.LoginDTO;
import org.wmq.shopmodel.Entity.system.SystemMenu;
import org.wmq.shopmodel.Entity.system.SystemUser;
import org.wmq.shopmodel.VO.system.LoginVO;
import org.wmq.shopmodel.VO.system.ValidateCodeVO;

import java.awt.*;

@RestController
@RequestMapping("/admin/system/index")
@RequiredArgsConstructor
public class IndexController {
    private final SystemUserService systemUserService;
    private final ValidateCodeService validateCodeService;
    private final SystemMenuService systemMenuService;

    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO){
        return this.systemUserService.login(loginDTO);
    }
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVO> generateValidateCode(){
        return Result.success(this.validateCodeService.generateValidateCode());
    }

    @GetMapping("/getSystemUserInfo")
    public Result<SystemUser> getSystemUserInfo(){
        return Result.success(this.systemUserService.getSystemUserInfo());
    }

    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(name = "token") String token){
        this.systemUserService.logout(token);
        return Result.success();
    }

    @GetMapping("/menus")
    public Result<List<SystemMenu>> getSystemUserMenus(){
        return Result.success(this.systemMenuService.getNodesBySystemUserId(SystemUserHolder.get().getId()));
    }
}
